<?php

namespace App\Services\SignIn;

use App\Models\Integral;
use App\Models\Sign;
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
use App\Services\Constant\Constants;
use App\Services\Constant\Formula;
use App\Services\Conversion\BinaryString;
use App\Services\Request;


=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
use App\Services\Constant\constants;
use App\Services\Conversion\BinaryString;
use App\Services\Request;

<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
class SignInService
{
    protected $redis;

    public function __construct()
    {
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1');
    }

    /**
     * @param $uid
     * @return array
     * @throws \RedisException
     * 用户签到，连续签到
     */
    public function signIn($uid)
    {
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        //获取当前时间
//        $nowTime = date('Y-m');
        $nowTime = date('2022-10');
        $day = 12;
<<<<<<< HEAD
=======
=======
<<<<<<< HEAD
        $nowTime = date('Y-m');
        $day = date('d');
=======
        //获取当前时间
//        $nowTime = date('Y-m');
        $nowTime = date('2022-10');
<<<<<<< HEAD
        $day = 1;
=======
        $day = 12;
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        //拼接bitmap的key
        $key = "sign:$uid:$nowTime";
        //查看是否签到过
        $isSign = $this->redis->getBit($key, $day);
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        //如果已签到则返回1，提示用户今天已经签到过了
        if ($isSign) {
            return 1;
        }
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        if ($isSign) {
            return Request::fail(-1, '今日已经签到', '');
        }
        //获取昨天时间
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        $yesterday = $day - 1;
        //查看昨天是否签到
        $isYesterday = $this->redis->getBit($key, $yesterday);
        $userSign = Sign::where('user_id', $uid)->first();
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        //如果昨天签到，则为连续签到
        if ($isYesterday) {
            $sign['sign_day'] = $userSign['sign_day'] + 1;
            //在原有的总积分上加连签的积分，总积分+(当前连签天数-1)*累加积分15+首签奖励
            $integral = $userSign['sum_score'] + Formula::signIntegral($sign['sign_day']);
            $this->integralInfo(2, $uid, $sign);
            if ($sign['sign_day'] == ConStants::FIRST_TIME) {//当用户连续签到天数达到7天，额外奖励50积分
                $integral = $integral + Constants::INTEGRAL_REWARD;
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        if ($userSign == null) {
            $userSign = Sign::create(['user_id' => $uid]);
        }
        //连续签到
        if ($isYesterday) {
            //在原有的天数的上+1
            $sign['sign_day'] = $userSign['sign_day'] + 1;
            //在原有的积分上+15
            $integral = $userSign['sum_score'] + ($sign['sign_day'] - 1) * 15 + 10;//总积分+(当前连签天数-1)*累加积分15+10
            //调用自定义方法添加积分明细
            $this->integralInfo(2, $uid, $sign);
            //如果用户连续签到天数达到7天，额外奖励7积分
            if ($sign['sign_day'] == ConStants::FIRST_TIME) {
                $integral = $integral + 50;
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
                $this->integralInfo(3, $uid, $sign);
            } elseif ($sign['sign_day'] == ConStants::SECOND_TIME) ;//连续签到14天额外赠送满50元-5元优惠券

            elseif ($sign['sign_day'] == ConStants::THIRD_TIME) ;//连续一个月+无条件5元优惠券
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
            $sign['sum_score'] = $integral;
            Sign::where('user_id', $uid)->update($sign);
            //该天进行签到
            $this->redis->setBit($key, $day, 1);
            return ['sum_score' => $sign, 'score' => Formula::signIntegral($sign['sign_day'])];
        }
        //断签，重新签到，连续签到天数变为1，总积分+10
        $sign['sign_day'] = 1;
        $sign['sum_score'] = $userSign['sum_score'] + Constants::FIRST_SIGN;
        Sign::where('user_id', $uid)->update($sign);
        $this->integralInfo(1, $uid, $sign);
        $this->redis->setBit($key, $day, 1);
        return ['sum_score' => $sign, 'score' => Constants::FIRST_SIGN];
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad

            $sign['sum_score'] = $integral;
            Sign::where('user_id', $uid)->update($sign);
            $this->redis->setBit($key, $day, 1);
            return Request::success(200, '连签成功', '');
        }
        //断签，重新签到
        $sign['sign_day'] = 1;
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        $sign['sum_score'] = $userSign['sum_score'] + 10;
        Sign::where('user_id', $uid)->update($sign);
        $this->integralInfo(1, $uid, $sign);
        $this->redis->setBit($key, $day, 1);
        return Request::success(200, '签到成功', '');
<<<<<<< HEAD
=======
=======
<<<<<<< HEAD
        //断签后，第一次签到积分+10
        $sign['sum_score'] = $userSign['sum_score'] + 10;
        Sign::where('user_id', $uid)->update($sign);
        //调用积分明细添加方法
=======
        $sign['sum_score'] = $userSign['sum_score'] + 10;
        Sign::where('user_id', $uid)->update($sign);
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
        $this->integralInfo(1, $uid, $sign);
        $this->redis->setBit($key, $day, 1);
        return Request::success(200, '签到成功', '');
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
    }

    /**
     * @param $uid
     * @param $date
     * @return string
     * @throws \RedisException
     * 获取用户当月签到信息
     */
    public function getSignInfo($uid, $date)
    {
<<<<<<< HEAD
        //获取签到
=======
<<<<<<< HEAD
        //获取签到
=======
<<<<<<< HEAD
        //获取签到时间
=======
        //获取签到
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        $key = "sign:$uid:$date";
        $sign = $this->redis->get($key);
        //处理数据
        $bitmap_bin_str = BinaryString::getSign($sign);
        //自动获取当月天数
        $monthSign = date('t');
        //循环查询本月签到情况
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        $sumDay = array();
        //将已签到对应数值存入到sumDay中
        for ($a = 1; $a <= $monthSign; $a++) {
            if ($bitmap_bin_str[$a] == 1) {
                $sumDay[] = $a;
            }
        }
        //调用向前获取连续签到天数，进行返回
        $i = $this->getBeforeSignDayInfo($uid, $date, date('d'), $i);
        if ($i == null) {
            $i = 0;
        }
        //获取当天是否签到
        $isSign = $this->redis->getBit($key, date('d'));
        $sum = Sign::where('user_id', $uid)->first();
        //返回连续签到天数和当天是否签到
        return ['sign_day' => $i, 'isSign' => $isSign, 'sumDay' => $sumDay, 'sumScore' => $sum['sum_score'], 'sign_day' => $sum['sign_day']];
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        for ($i = 1; $i <= $monthSign; $i++) {
            if ($bitmap_bin_str[$i] != 1) {
                echo "$i 日未签到";
                echo "<br>";
            } else {
                echo "$i 日签到";
                echo "<br>";
            }
        }
        return $bitmap_bin_str;
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
    }

    /**
     * @param $uid
     * @param $date
     * @return array
     * @throws \RedisException
     * 用户补签
     */
    public function getRetroactive($uid, $date)
    {
        $nowTime = date('Y-m-d');
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        //如果当前日期是否小于等于补签日期，则返回1
        if ($nowTime <= $date) {
            return 1;
        }
        //获取用户总积分
        $res = Sign::where('user_id', $uid)->first()->sum_score;
        //如果当前用户总积分小于补签积分要求，返回2
        if ($res < Constants::RETROACTIVE_INTEGRAL) {
            return 2;
        }
        //截取补签的年月，日，获取补签那天签到信息
        $month = substr($date, 0, 7);
        $day = substr($date, 8, 2);
        $key = "sign:$uid:$month";
        $sign = $this->redis->getBit($key, $day);
        //如果用户已经签到过了，则返回
        if ($sign == 1) {
            return 3;
        }
        //如果补签那天没签到，直接进行签到
        $this->redis->setBit($key, $day, 1);
        //获取当前时间，获取当前天数签到信息
        $now = date('Y-m');
        $day = date('d');
        $key = "sign:$uid:$now";
        $sign = $this->redis->getBit($key, $day);
        //如果当前日期没有签到，就向前获取进行计算连续签到天数，并且重置积分进行计算及记录积分明细
        if ($sign != 1) {
            //调用往前获取签到天数方法，天数-1
            $data = self::getBeforeSignDayInfo($uid, $month, $day - 1, $i);
            Sign::where('user_id', $uid)->update(['sign_day' => $data == null ? 0 : $data]);
            //调用重置积分方法，传入补签日期
            $user = $this->resetIntegral($uid, $date);
            $integral = $user['sum_score'] - Constants::RETROACTIVE_INTEGRAL;//补签扣除30积分
            Sign::where('user_id', $uid)->update(['sum_score' => $integral]);
            $this->integralInfo(4, $uid, $integral);
            return ['sum_score'=>$integral,'sign_day'=>$data==null?0:$data];
        }
        //如果当前已经签到则调用往前获取签到天数方法
        $data = self::getBeforeSignDayInfo($uid, $month, $day, $i);
        Sign::where('user_id', $uid)->update(['sign_day' => $data]);
        //调用重置积分方法，传入补签日期
        $user = $this->resetIntegral($uid, $date);
        $integral = $user['sum_score'] - Constants::RETROACTIVE_INTEGRAL;
        Sign::where('user_id', $uid)->update(['sum_score' => $integral]);
        //补签成功，调用积分明细方法，进行记录
        $this->integralInfo(4, $uid, $integral);
        return ['sum_score'=>$integral,'sign_day'=>$data==null?0:$data];
    }

    /**+
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
//        dd($date);
        //判断当前日期是否小于补签日期
        if ($nowTime <= $date) {
            return Request::fail('-1', '补签日期不能超过今天', '');
        }
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        //截取补签日期年月日
        $month = substr($date, 0, 7);
        $day = substr($date, 8, 2);
        //获取补签日期签到信息
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        //截取年月日
        $month = substr($date, 0, 7);
        $day = substr($date, 8, 2);
        //获取当天签到信息
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        $key = "sign:$uid:$month";
        $sign = $this->redis->getBit($key, $day);
//        return $sign;
        //判断该天用户是否已经签到过了
//        dd($day);
        if ($sign == 1) {
            return Request::fail('-1', '该天已经签到过了', '');
        }
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        //如果补签那天没签到，直接进行补签
        $this->redis->setBit($key, $day, 1);
        //获取当前时间
        $now = date('Y-m');
        $day = date('d');
        //拼接bitmap的key
        $key = "sign:$uid:$now";
        //获取当前现在签到信息
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        //如果当天没签到，直接进行签到
        $this->redis->setBit($key, $day, 1);
        //当前时间
        $key = "sign:$uid:$month";
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        $sign = $this->redis->getBit($key, $day);
//        return $sign;
//        if ($sign != 1) {
//            //调用往前获取签到天数方法
//            $data = self::getBeforeSignDayInfo($uid, $month, $day - 1, $i);
//            Sign::where('user_id', $uid)->update(['sign_day' => $data]);
//            $user = $this->resetIntegral($uid, $date);
//            $integral = $user['sum_score'] - 30;//补签扣除30积分
//            //修改签到表的总积分
//            Sign::where('user_id', $uid)->update(['sum_score', $integral]);
//            //补签成功，调用积分明细方法，进行记录
//            $this->integralInfo(4, $uid, $integral);
//            return Request::success(200, '补签成功', '');
//        }
<<<<<<< HEAD
        //调用往前获取签到天数方法
        $data = self::getBeforeSignDayInfo($uid, $month, $day - 1, $i);
=======
<<<<<<< HEAD
        //调用往前获取签到天数方法
        $data = self::getBeforeSignDayInfo($uid, $month, $day - 1, $i);
=======
<<<<<<< HEAD

        //修改补签后的连续签到天数，结算补签奖励分数

        //调用往前获取签到天数方法
        $data = self::getBeforeSignDayInfo($uid, $month, $day - 1, $i);
        //修改连签天数
=======
        //调用往前获取签到天数方法
        $data = self::getBeforeSignDayInfo($uid, $month, $day - 1, $i);
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        Sign::where('user_id', $uid)->update(['sign_day' => $data]);
        $user = $this->resetIntegral($uid, $date);
        //补签扣除总积分的30积分
        $integral = $user['sum_score'] - 30;
        //修改签到表的总积分
        Sign::where('user_id', $uid)->update(['sum_score' => $integral]);
        //补签成功，调用积分明细方法，进行记录
        $this->integralInfo(4, $uid, $integral);
        return Request::success(200, '补签成功', '');
    }

    /**
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
     * @param $uid
     * @param $date
     * @param $day
     * @param $i
     * @return array|int|mixed
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
     * 获取补签前面天数签到信息，用户连续签到天数
     */
    public function getBeforeSignDayInfo($uid, $date, $day, &$i)
    {
        //bitmap的key，补签的年月
        $key = "sign:$uid:$date";
        $sign = $this->redis->getBit($key, $day);
        //如果那天当前用户已经签到过
        if ($sign == 1) {
            $i++;
            if ($day - 1 > 0) {//补签那天-1大于0，则证明还是在本月，继续递归调用，并且天数-1
                $this->getBeforeSignDayInfo($uid, $date, $day - 1, $i);
            } else {//否则当前天数小于或者等于0，则计算上个月的签到状况
                $time = strtotime($date . '-' . '0' . $day) - 86400;
                $myTime = date('Y-m-d', $time);
                $day = substr($myTime, 8, 2);
                $month = substr($myTime, 0, 7);
                self::getBeforeSignDayInfo($uid, $month, $day, $i);//递归重复调用
            }
        }
        return $i;//返回连签天数
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
     * 获取补签前天数签到信息，用户连续签到天数
     */
    public function getBeforeSignDayInfo($uid, $date, $day, &$i)
    {
//        dd($day);
        try {
            //bitmap的key
            $key = "sign:$uid:$date";
            //获取签到状态
            $sign = $this->redis->getBit($key, $day);
//            dd($sign);
            //如果该天当前用户已经签到过
            if ($sign == 1) {
                $i++;//连续签到天数++
                if ($day - 1 > 0) {//当前天数-1大于0，继续递归调用
                    self::getBeforeSignDayInfo($uid, $date, $day - 1, $i);
                } else {//否则当前天数小于或者等于0，则计算上个月的签到状况
//                    return $date .'-'. '0' .$day;
                    $time = strtotime($date . '-' . '0' . $day) - 86400;//拼接日期格式，并减去一天
//                    echo 111;
//                    return $time;
                    $myTime = date('Y-m-d', $time);//上个月最后一天
//                    return $mytime;
                    $day = substr($myTime, 8, 2);
                    $month = substr($myTime, 0, 7);
                    self::getBeforeSignDayInfo($uid, $month, $day, $i);//递归重复调用
//                    return $time;
                }
            }
            return $i;//返回连签天数
        } catch (\Exception $exception) {
            return Request::fail('-1', $exception->getMessage(), '');
        }
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
    }

    /**
     * @param $uid
     * @param $date
     * @param $day
     * @param $i
     * @return array|int|mixed
     * 获取补签后面天数的签到信息，用户连续签到天数
     */
    public function getAfterSignDayInfo($uid, $date, $day, &$i)
    {
<<<<<<< HEAD
        try {
            //获取当前补签的年月日
            $nowMonth = substr($date,0,7);
=======
<<<<<<< HEAD
        try {
            //获取当前补签的年月日
            $nowMonth = substr($date,0,7);
=======
<<<<<<< HEAD
        //获取当前补签的年月日
        $nowMonth = substr($date, 0, 7);
        $time = strtotime($nowMonth);
        //自动计算补签当月天数
        $number = date('t', $time);
        $key = "sign:$uid:$date";
        $sign = $this->redis->getBit($key, $day);
        //如果当天签到了
        if ($sign == 1) {
            $i++;
            if ($day + 1 <= $number) {//当前天数+1小于补签当月的天数，则一直递归调用本方法
                self::getAfterSignDayInfo($uid, $date, $day + 1, $i);
            } else {//如果大于当月天数则获取到下个月第一天
                $time = strtotime($date . '-' . $day) + 86400;
                $myTime = date('Y-m-d', $time);
                $day = substr($myTime, 8, 2);
                $month = substr($myTime, 0, 7);
                self::getAfterSignDayInfo($uid, $month, $day, $i);//递归调用
            }
        }
        return $i;//返回向后连续签到天数
=======
        try {
            //获取当前补签的年月日
            $nowMonth = substr($date,0,7);
<<<<<<< HEAD
            //获取当月时间戳
=======
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
            $time = strtotime($nowMonth);
            //自动计算补签当月天数
            $number = date('t', $time);
            //拼接bitmap的key
            $key = "sign:$uid:$date";
            $sign = $this->redis->getBit($key, $day);
            //如果当天签到了
            if ($sign == 1) {
<<<<<<< HEAD
                $i++;
                if ($day + 1 <= $number) {//当前天数+1小于补签当月的天数，则一直递归调用本方法
=======
<<<<<<< HEAD
                $i++;
                if ($day + 1 <= $number) {//当前天数+1小于补签当月的天数，则一直递归调用本方法
=======
<<<<<<< HEAD
                $i++;//连续签到天数
                if ($day + 1 <= $number) {//当前天数+1小于等于补签当月的天数，则一直递归调用本方法
=======
                $i++;
                if ($day + 1 <= $number) {//当前天数+1小于补签当月的天数，则一直递归调用本方法
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
                    self::getAfterSignDayInfo($uid, $date, $day + 1, $i);
                } else {//如果大于则自动跳转到下个月
                    $time = strtotime($date . '-' . '0' . $day) + 86400;
                    $myTime = date('Y-m-d', $time);
                    $day = substr($myTime, 8, 2);
                    $month = substr($myTime, 0, 7);
                    self::getAfterSignDayInfo($uid, $month, $day, $i);//递归调用
                }
            }
            return $i;
        } catch (\Exception $exception) {
            return Request::fail('-1', $exception->getMessage(), '');
        }
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
    }

    /**
     * @param $uid
     * @param $date
     * @return mixed
     * @throws \RedisException
     * 补签重置积分
     */
    public function resetIntegral($uid, $date)
    {
        //获取日期
        $month = substr($date, 0, 7);
        $day = substr($date, 8, 2);
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        //拼接bitmap的key，补签的年月
        $key = "sign:$uid:$month";
        //定义前后连续签到天数变量
        $beforeNumber = 0;
        $afterNumber = 0;
        //定义前后连续签到积分变量
        $beforeIntegral = 0;
        $afterIntegral = 0;
        //获取补签前一天签到信息
        $beforeDayNumber = $day - 1;
        //如果补签那天是1号，就获取上个月的最后一天
        if ($beforeDayNumber <= 0) {
            $time = strtotime($date) - 86400;
            $myTime = date('Y-m-d', $time);
            $day = substr($myTime, 8, 2);
            $month = substr($myTime, 0, 7);
            $key = "sign:$uid:$month";
            $beforeDayNumber = $day;
        }
        //根据那天获取签到信息
        $sign = $this->redis->getBit($key, $beforeDayNumber);
        //如果签到了
        if ($sign == 1) {
            //调用获取向前连续签到天数方法
            $i = $this->getBeforeSignDayInfo($uid, $month, $beforeDayNumber, $i);
            $beforeNumber = $i;//连签天数
        }
        //获取补签后一天是否为签到
        $day = substr($date, 8, 2);
        $afterDayNumber = $day + 1;
        //如果补签那天大于当月天数，则会把补签日期变为时间戳进行+1天，变成下月一号
        if ($afterDayNumber > date('t', strtotime($date))) {
            $time = strtotime($date) + 86400;
            $myTime = date('Y-m-d', $time);
            $day = substr($myTime, 8, 2);
            $month = substr($myTime, 0, 7);
            $key = "sign:$uid:$month";
            $afterDayNumber = $day;
        }
        $sign = $this->redis->getBit($key, $afterDayNumber);
        if ($sign == 1) {
            $i = 0;
            //调用获取向后连续签到天数方法
            $i = $this->getAfterSignDayInfo($uid, $month, $afterDayNumber, $i);
            $afterNumber = $i;//连签天数
        }
        //计算补签后签到天数，向前连续天数+向后连续天数+补签当天
        $sumDay = $beforeNumber + $afterNumber + 1;
        //计算补签后签到奖励分，补签后签到积分-补签前签到积分
        $sumIntegral = Formula::evenSign($sumDay) - Formula::evenSign($sumDay - 1);
        //记录补签奖励积分和用户总积分，进行返回
        $sign = Sign::where('user_id', $uid)->first()->toArray();
        $sign['integral'] = $sumIntegral;
        $sign['sum_score'] = $sign['sum_score'] + $sumIntegral;
        $this->integralInfo(5, $uid, $sign);
        return $sign;
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        //拼接bitmap的key
        $key = "sign:$uid:$month";
//        return $key;
        //前后连续签到天数
        $beforeNumber = 0;
        $afterNumber = 0;
        //前后连续签到积分
        $beforeIntegral = 0;
        $afterIntegral = 0;
        //获取补签前一天签到信息
        $beforeNumber = $day - 1;
        $sign = $this->redis->getBit($key, $beforeNumber);
        if ($sign == 1) {
            //调用获取向前连续签到天数方法
            $i = $this->getBeforeSignDayInfo($uid, $month, $beforeNumber, $i);
//            return $i;
            $beforeNumber = $i;//连签天数
            $beforeIntegral = (5 + 15 * $i) * $i / 2;//积分
//            return $beforeNumber;
        }
        //获取补签后一天是否为签到
        $afterNumber = $day + 1;
        $sign = $this->redis->getBit($key, $afterNumber);
        if ($sign == 1) {
            $i = 0;
//            return $i;
            //调用获取向后连续签到天数方法
            $i = $this->getAfterSignDayInfo($uid, $month, $afterNumber, $i);
            $afterNumber = $i;//连签天数
            $afterIntegral = (5 + 15 * $i) * $i / 2;//（10＋（10＋15*（$i－1）））*$i/2起始签到积分+（起始积分+差集*（连签-1））*连签/2
//            return $afterIntegral;
        }
        //计算签到天数
        $sumDay = $beforeNumber + $afterNumber + 1;
        //计算签到的积分
        $sumIntegral = (5 + 15 * $sumDay) * $sumDay / 2;
//        return $sumIntegral;
        $sign = Sign::where('user_id', $uid)->first()->toArray();
//        echo $beforeIntegral;
//        echo $afterIntegral;
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
        //重置补签后的积分
=======
>>>>>>> ba16dd1753d15808db3b29ea7d71d2dcbc440fff
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        $sign['sum_score'] = $sign['sum_score'] - $beforeIntegral - $afterIntegral;
        $sign['sum_score'] = $sign['sum_score'] + $sumIntegral;
//        $this->integralInfo(5,$uid,$sign['sum_score']);
        return $sign;

<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
    }

    /**
     * @param $type
     * @param $uid
     * @param $update
     * @param $integral
     * @return void
     * 用户积分明细添加
     */
    public function integralInfo($type, $uid, $sign)
    {
        //1.签到2.连续签到3.签到七天4.补签
        if ($type == 1) {
            $data = [
                'user_id' => $uid,
                'content' => '用户签到' . $sign['sign_day'] . '天奖励积分:10',
                'type' => 0,
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
                'score' => Constants::FIRST_SIGN,
            ];
        } elseif ($type == 2) {
            $num = Formula::signIntegral($sign['sign_day']);
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
                'score' => 10,
            ];
        } elseif ($type == 2) {
            $num = ($sign['sign_day'] - 1) * 15 + 10;
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
            $data = [
                'user_id' => $uid,
                'content' => '用户连续签到' . $sign['sign_day'] . '天奖励积分:' . $num,
                'type' => 0,
<<<<<<< HEAD
                'score' => ($sign['sign_day'] - 1) * 15 + 10,
=======
<<<<<<< HEAD
                'score' => ($sign['sign_day'] - 1) * 15 + 10,
=======
<<<<<<< HEAD
                'score' => $num,
=======
                'score' => ($sign['sign_day'] - 1) * 15 + 10,
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
            ];
        } elseif ($type == 3) {
            $data = [
                'user_id' => $uid,
                'content' => '用户连续签到7天额外奖励积分:50',
                'type' => 0,
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
                'score' => Constants::INTEGRAL_REWARD,
            ];
        } elseif ($type == 5) {
            $data = [
                'user_id' => $uid,
                'content' => '用户补签奖励积分：' . $sign['integral'],
                'type' => 0,
                'score' => $sign['integral'],
=======
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
                'score' => 50,
            ];
        } elseif ($type == 5) {
            $num = ($sign['sign_day'] - 1) * 15 + 10;
            $data = [
                'user_id' => $uid,
                'content' => '用户补签奖励积分：' . $num,
                'type' => 0,
                'score' => $num
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
            ];
        } else {
            $data = [
                'user_id' => $uid,
                'content' => '用户补签使用30积分',
                'type' => 0,
<<<<<<< HEAD
                'score' => -30
            ];
        }
=======
<<<<<<< HEAD
                'score' => -30
            ];
        }
=======
<<<<<<< HEAD
                'score' => -Constants::THIRD_TIME
            ];
        }
        //记录到积分明细表
=======
                'score' => -30
            ];
        }
>>>>>>> ad3f2ce75fb6a6518d685720d3e13e7eb6b3b462
>>>>>>> 5b8878cf77da32ab9fb02103e80d26f0a43187e4
>>>>>>> 75818497ef9bb23fd6bd1547c9e88686d864b7ad
        Integral::create($data);
    }
}